<template>
  <div class="login-container">
    <!-- 背景装饰 -->
    <div class="background-decoration">
      <div class="chinese-character">文</div>
      <div class="traditional-building"></div>
      <div class="pattern-overlay"></div>
    </div>

    <!-- 登录表单 -->
    <div class="login-form-container">
      <div class="login-form">
        <h1 class="form-title">非物质文化遗产系统</h1>
        
        <form @submit.prevent="handleLogin">
          <div class="form-group">
            <div class="input-wrapper">
              <i class="icon user-icon">👤</i>
              <input
                v-model="loginForm.username"
                type="text"
                placeholder="请输入用户名"
                class="form-input"
                required
              />
            </div>
          </div>

          <div class="form-group">
            <div class="input-wrapper">
              <i class="icon lock-icon">🔒</i>
              <input
                v-model="loginForm.password"
                type="password"
                placeholder="请输入密码"
                class="form-input"
                required
              />
            </div>
          </div>

          <div class="form-group">
            <div class="role-selector">
              <select v-model="loginForm.role" class="role-select">
                <option value="admin">管理员</option>
                <option value="user">普通用户</option>
              </select>
              <i class="icon arrow-icon">⌄</i>
            </div>
          </div>

          <button type="submit" class="login-btn">
            登录
          </button>
        </form>

        <div class="register-link">
          还没有账号？<router-link to="/register" class="link">请注册</router-link>
        </div>
      </div>
    </div>

    <!-- 底部装饰 -->
    <div class="bottom-decoration">
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { login } from '../api/auth'

const router = useRouter()

const loginForm = ref({
  username: '',
  password: '',
  role: 'admin'
})

const handleLogin = async () => {
  try {
    const response = await login(loginForm.value)
    if (response.success === true || response.errorCode === 20000){
      ElMessage.success('登录成功')
      // 存储token - 注意：根据后端返回格式，token直接在data字段中
      localStorage.setItem('token', response.data)
      // 存储用户角色
      localStorage.setItem('userRole', loginForm.value.role)
      // 根据角色跳转到不同页面
      if (loginForm.value.role === 'admin') {
        // 管理员跳转到后台管理页面
        router.push('/admin/dashboard')
      } else {
        // 普通用户跳转到首页
        router.push('/home')
      }
    } else {
      ElMessage.error(response.message || '登录失败')
    }
  } catch (error) {
    console.error('登录错误:', error)
    ElMessage.error('登录失败，请检查网络连接')
  }
}
</script>

<style scoped>
.login-container {
  position: relative;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.background-decoration {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

.chinese-character {
  position: absolute;
  top: 10%;
  right: 15%;
  font-size: 200px;
  font-weight: bold;
  color: rgba(139, 28, 28, 0.3);
  font-family: 'SimSun', serif;
  transform: rotate(-15deg);
  user-select: none;
}

.traditional-building {
  position: absolute;
  left: 5%;
  top: 20%;
  width: 300px;
  height: 400px;
  background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 200 250"><rect x="20" y="150" width="160" height="80" fill="%23667eea" opacity="0.3"/><polygon points="10,150 100,80 190,150" fill="%23667eea" opacity="0.4"/><polygon points="30,130 100,70 170,130" fill="%23667eea" opacity="0.5"/><polygon points="50,110 100,60 150,110" fill="%23667eea" opacity="0.6"/></svg>') no-repeat center;
  background-size: contain;
  opacity: 0.6;
}

.pattern-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: 
    radial-gradient(circle at 20% 20%, rgba(255,255,255,0.1) 1px, transparent 1px),
    radial-gradient(circle at 80% 80%, rgba(255,255,255,0.1) 1px, transparent 1px);
  background-size: 50px 50px;
  opacity: 0.3;
}

.login-form-container {
  position: relative;
  z-index: 2;
  margin-left: auto;
  margin-right: 10%;
}

.login-form {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 40px;
  width: 400px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.form-title {
  text-align: center;
  color: #333;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 30px;
  font-family: 'Microsoft YaHei', sans-serif;
}

.form-group {
  margin-bottom: 20px;
}

.input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.icon {
  position: absolute;
  left: 15px;
  font-size: 18px;
  z-index: 3;
  pointer-events: none;
}

.form-input {
  width: 100%;
  padding: 15px 15px 15px 50px;
  border: 2px solid #e5e7eb;
  border-radius: 10px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: white;
  box-sizing: border-box;
}

.form-input:focus {
  outline: none;
  border-color: #dc2626;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1);
}

.role-selector {
  position: relative;
  display: flex;
  align-items: center;
}

.role-select {
  width: 100%;
  padding: 15px 50px 15px 15px;
  border: 2px solid #e5e7eb;
  border-radius: 10px;
  font-size: 16px;
  background: white;
  appearance: none;
  cursor: pointer;
  transition: all 0.3s ease;
}

.role-select:focus {
  outline: none;
  border-color: #dc2626;
  box-shadow: 0 0 0 3px rgba(220, 38, 38, 0.1);
}

.arrow-icon {
  position: absolute;
  right: 15px;
  font-size: 16px;
  color: #6b7280;
  pointer-events: none;
}

.login-btn {
  width: 100%;
  padding: 15px;
  background: #dc2626;
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 18px;
  font-weight: bold;
  cursor: pointer;
  transition: all 0.3s ease;
  margin-top: 10px;
}

.login-btn:hover {
  background: #b91c1c;
  transform: translateY(-2px);
  box-shadow: 0 10px 20px rgba(220, 38, 38, 0.3);
}

.register-link {
  text-align: center;
  margin-top: 20px;
  color: #6b7280;
  font-size: 14px;
}

.link {
  color: #2563eb;
  text-decoration: none;
  font-weight: bold;
}

.link:hover {
  text-decoration: underline;
}

.bottom-decoration {
  position: absolute;
  bottom: 20px;
  right: 20px;
  z-index: 2;
}

.video-icon {
  width: 50px;
  height: 50px;
  background: white;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.2);
  cursor: pointer;
  transition: all 0.3s ease;
}

.video-icon:hover {
  transform: scale(1.1);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}

@media (max-width: 768px) {
  .login-form-container {
    margin-right: 5%;
  }
  
  .login-form {
    width: 350px;
    padding: 30px;
  }
  
  .traditional-building {
    width: 200px;
    height: 300px;
  }
  
  .chinese-character {
    font-size: 120px;
  }
}
</style>
